/*改变所购商品的数量*/
function changeNum(numId,flag){
	let num = parseInt(document.getElementById(numId).value)
    console.log(num)
    if(flag == "add"){
        num += 1
    }else if(flag == "minus"){
        if(num == 1){
            alert("宝贝数量必须大于0")
            return
        }
        num -= 1
    }
    document.getElementById(numId).value = num

    productCount()
}

/*自动计算商品的总金额、总共节省的金额和积分*/
//import { total, integral, point, number, subtotal } from './productCount.js';
function productCount(){
   const trlist = document.getElementsByTagName("tr")
    const trproducts = []
    for (const tr of trlist) {
        if(tr.id != ""){
            trproducts.push(tr)
        }
    }
    //总计
    let zongji = 0;
    for (const trproduct of trproducts) {

        const price = trproduct.getElementsByClassName("cart_td_5")[0].innerText * 1
        const num = trproduct.getElementsByClassName("num_input")[0].value * 1
        //小计
        const xiaoji = price*num;
        const isSelect = trproduct.firstElementChild.firstElementChild.checked
        if(isSelect){
            zongji += xiaoji
        }

        // console.log(price,num,price*num)
        trproduct.getElementsByClassName("cart_td_7")[0].innerText = xiaoji
    }
    document.getElementById("total").innerText = zongji
}


/*复选框全选或全不选效果*/
function selectAll(){
	const selectAll = document.getElementById("allCheckBox").checked
    const cartCheckBoxlist = document.getElementsByName("cartCheckBox");
    for (const cartCheckBoxlistElement of cartCheckBoxlist) {
        cartCheckBoxlistElement.checked = selectAll
    }
    productCount()
}
	
/*根据单个复选框的选择情况确定全选复选框是否被选中*/
function selectSingle(){
    let selectAll = true
    const cartCheckBoxlist = document.getElementsByName("cartCheckBox");
    for (const cartCheckBoxlistElement of cartCheckBoxlist) {
        selectAll = selectAll && cartCheckBoxlistElement.checked
    }
    document.getElementById("allCheckBox").checked = selectAll

    productCount()
}

/*删除单行商品*/
function deleteRow(rowId){
    const tr1 = document.getElementById(rowId)
    //前一个兄弟节点
    const tr2 = tr1.previousElementSibling
    tr1.parentNode.removeChild(tr1)
    tr2.parentNode.removeChild(tr2)
    productCount()
}

/*删除选中行的商品*/
function deleteSelectRow(){
	//获取全部多选框(排除全选)
    // 排除没有选中
    // 获取当前多选框所在tr的id
    // 调用deleteRow方法
    const cartCheckBoxlist = document.getElementsByName("cartCheckBox");
    //需要被删除的tr的id
    const ids = []
    for (const cartCheckBoxlistElement of cartCheckBoxlist) {
        const tr = cartCheckBoxlistElement.parentNode.parentNode
        // console.log(tr,cartCheckBoxlistElement.checked)
        if(cartCheckBoxlistElement.checked){
            ids.push(tr.id)
        }
    }
    for (const id of ids) {
        deleteRow(id)
    }
}

function changeTab(index){
    for (let i = 1; i <= 3; i++) {
        if(i == index){
            document.getElementById('cont'+i).style.display = 'block'
        }else{
            document.getElementById('cont'+i).style.display = 'none'
        }
    }
}


window.onload = function (){
    productCount()
    changeTab(1)
}
